package com.zhuyiyi.controller;

import com.zhuyiyi.entity.Menu;
import com.zhuyiyi.entity.MenuVO;
import com.zhuyiyi.feign.MenuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/menu")
public class MenuHandle {

    @Autowired
    private MenuFeign menuFeign;


    @ResponseBody
    @GetMapping("/findAll")
    public MenuVO findAll(@RequestParam("page")  int page,
                          @RequestParam("limit") int limit){
        int index=(page-1)*limit;
        return menuFeign.findAll(index,limit);
    }


    @GetMapping("/prepareSave")
    public String prepareSave(Model model){
        model.addAttribute("list",menuFeign.findAll());
        return "menu_add";
    }

    @GetMapping("/redirect/{location}")
    public String location(@PathVariable("location") String location){
        return location;
    }

    @PostMapping("/save")
    public String save(Menu menu){
        menuFeign.save(menu);
        return "menu_manage";
    }

    @GetMapping("/findById/{id}")
    public String findById(@PathVariable("id") long id,Model model){
        model.addAttribute("list",menuFeign.findAll());
        model.addAttribute("menu",menuFeign.findById(id));
        return "menu_update";
    }

    @PostMapping("/update")
    public String update(Menu menu){
        menuFeign.update(menu);
        return "menu_manage";
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        menuFeign.deleteById(id);
        return "menu_manage";
    }
}
